package com.yike.user.service.impl;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.yike.common.exception.BusinessException;
import com.yike.common.tool.LoginUtil;
import com.yike.user.entity.ShopPayChannel;
import com.yike.user.mapper.ShopPayChannelMapper;
import com.yike.user.param.ShopPayChannelParam;
import com.yike.user.service.ShopPayChannelService;
import com.yike.user.param.ShopPayChannelPageParam;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.yike.common.base.BaseServiceImpl;
import com.yike.common.pagination.Paging;
import com.yike.common.pagination.PageInfo;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.metadata.OrderItem;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yike.user.vo.ShopPayChannelVo;
import org.springframework.beans.BeanUtils;
import org.springframework.transaction.annotation.Transactional;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.Date;

/**
 * 店铺支付渠道 服务实现类
 *
 * @author Sakura
 * @since 2024-11-15
 */
@Slf4j
@Service
public class ShopPayChannelServiceImpl extends BaseServiceImpl<ShopPayChannelMapper, ShopPayChannel> implements ShopPayChannelService {

    @Autowired
    private ShopPayChannelMapper shopPayChannelMapper;

    @Transactional(rollbackFor = Exception.class)
    @Override
    public boolean saveShopPayChannel(ShopPayChannelParam shopPayChannelParam) throws Exception {
        // 每个店铺只能同时存在一个有效支付渠道，所以如果当前支付渠道是有效的则需要关闭其它有效渠道
        if (shopPayChannelParam.getStatus() == 1) {
            shopPayChannelMapper.update(
                    null,
                    Wrappers.lambdaUpdate(ShopPayChannel.class)
                            .set(ShopPayChannel::getStatus, 0)
                            .set(ShopPayChannel::getUpdateBy, LoginUtil.getUserId())
                            .set(ShopPayChannel::getUpdateTime, new Date())
                            .eq(ShopPayChannel::getShopId, shopPayChannelParam.getShopId())
                            .eq(ShopPayChannel::getStatus, 1));
        }
        ShopPayChannel shopPayChannel = new ShopPayChannel();
        BeanUtils.copyProperties(shopPayChannelParam, shopPayChannel);
        shopPayChannel.setCreateBy(LoginUtil.getUserId());
        return super.save(shopPayChannel);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public boolean updateShopPayChannel(ShopPayChannelParam shopPayChannelParam) throws Exception {
        ShopPayChannel shopPayChannel = shopPayChannelMapper.selectById(shopPayChannelParam.getId());
        if (shopPayChannel == null) {
            throw new BusinessException("支付渠道信息异常");
        }

        // 每个店铺只能同时存在一个有效支付渠道，所以如果当前支付渠道是有效的则需要关闭其它有效渠道
        if (shopPayChannelParam.getStatus() == 1) {
            shopPayChannelMapper.update(
                    null,
                    Wrappers.lambdaUpdate(ShopPayChannel.class)
                            .set(ShopPayChannel::getStatus, 0)
                            .set(ShopPayChannel::getUpdateBy, LoginUtil.getUserId())
                            .set(ShopPayChannel::getUpdateTime, System.currentTimeMillis())
                            .eq(ShopPayChannel::getShopId, shopPayChannelParam.getShopId())
                            .eq(ShopPayChannel::getStatus, 1)
                            .ne(ShopPayChannel::getId, shopPayChannelParam.getId()));
        }

        BeanUtils.copyProperties(shopPayChannelParam, shopPayChannel);
        shopPayChannel.setUpdateBy(LoginUtil.getUserId());
        return super.updateById(shopPayChannel);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public boolean deleteShopPayChannel(Long id) throws Exception {
        return super.removeById(id);
    }

    @Override
    public ShopPayChannelVo getShopPayChannel(Long id) throws Exception {
        return shopPayChannelMapper.getShopPayChannelVo(id);
    }

    @Override
    public Paging<ShopPayChannelVo> getShopPayChannelPageList(ShopPayChannelPageParam shopPayChannelPageParam) throws Exception {
        Page<ShopPayChannel> page = new PageInfo<>(shopPayChannelPageParam);
        IPage<ShopPayChannelVo> iPage = shopPayChannelMapper.getShopPayChannelList(page, shopPayChannelPageParam);
        return new Paging<ShopPayChannelVo>(iPage);
    }

}
